import Vue from 'vue';
import Vuex from 'vuex';
import lodash from 'lodash';
import { getLabelData } from '@/api/openApi';
import { getAuthMenuData } from '@/api/loginRegister';
import { queryAttachPath } from '@/api/system/tbSysAttachment';
import { getDictDetailList } from '@/api/system/tbSysDictDetail';
import { getDictDefineList } from '@/api/system/tbSysDictDefinition';
import { siteBaseInfo, getDictConstByCode, getDictEnumByCode } from '@/api/openApi';
Vue.use(Vuex)

const state = {
	token: '',
	isGray: '',
	siteInfo: {},
	userInfo: {},
	menuData: [],
	dictEnums: {},
	dictConst: {},
	authButton: [],
	courseLabel: []
}

const actions = {
	init: ({ getters, dispatch }) => {
		return new Promise(async (resolve, reject) => {
			await dispatch('judgeSiteGray');
			await dispatch('getSiteBaseInfo');
			await dispatch('getSiteBaseTheme');
			if (!!getters.token) {
				await dispatch('getCourseLabel');
			}
			resolve();
		}).catch(() => {
			reject();
		})
	},

	//获取站点基本信息
	async getSiteBaseInfo({ commit, dispatch }) {
		let params = { page: 1, limit: 9999 };
		let result = await siteBaseInfo(params).then(res => res.data);
		if (result.code === '0') {
			let siteInfo = {};
			let datas = result.data || [];
			datas.forEach(item => {
				let fieldValue = '';
				if (item.type === '1') {
					fieldValue = item.value;
				} else if (item.type === '2') {
					let httpUrl = item.sysAttachment?.httpUrl;
					fieldValue = httpUrl?.replaceIP();
				}
				siteInfo[item.code] = fieldValue;
			})
			document.title = siteInfo.webName;
			document.getElementById('favicon').href = siteInfo.webFavicon;
			commit('SET_SITE_INFO', siteInfo);
		}
	},

	//网站是否灰掉
	async judgeSiteGray({ dispatch, commit }) {
		let datas = await dispatch('getConstByCode', 'GRAY_SITE');
		commit('SET_SITE_GRAY', !!datas && datas === '1');
	},

	//网站主题
	async getSiteBaseTheme({ dispatch, commit }) {
		let datas = await dispatch('getConstByCode', 'THEME_ACTIVE_HOME');
		commit('SET_SITE_THEME', datas);
	},

	//通过CODE获取字典常量数据
	async getConstByCode({ commit }, code) {
		let result = await getDictConstByCode(code).then(res => res.data);
		if (result.code === '0') {
			let datas = Vue.prototype.$detailData(result.data);
			return Promise.resolve({ code, value: datas });
		}
	},

	//通过CODE获取字段枚举数据
	async getEnumByCode({ commit }, code) {
		let result = await getDictEnumByCode(code).then(res => res.data);
		if (result.code === '0') {
			return Promise.resolve({ code, value: result.data || [] });
		}
	},

	//获取权限菜单
	async getAuthMenu({ commit }, id) {
		let result = await getAuthMenuData(id).then(res => res.data);
		if (result.code === '0') {
			let datas = Vue.prototype.$detailData(result.data);
			let { menuToRouter } = require('@/utils/getRouter');
			commit('SET_MENU_DATA', new menuToRouter(datas.menu));
			commit('SET_AUTH_BUTTON', datas.button);
		}
	},

	//获取课程标签
	async getCourseLabel({ commit }) {
		let params = { type: 6, labelType: 1 };
		let result = await getLabelData(params).then(res => res.data);
		if (result.code === '0') {
			commit('SET_COURSE_LABEL', result.data || []);
		}
	},

	//获取附件地址
	async getFileUrl({ commit }, id) {
		let result = await queryAttachPath(id).then(res => res.data);
		if (result.code === '0') {
			let datas = Vue.prototype.$detailData(result.data);
			return Promise.resolve(lodash.isEmpty(datas) ? '' : datas);
		}
	}
}

const mutations = {
	SET_TOKEN: (state, d) => {
		state.token = d;
		Vue.prototype.$session.set('token', d);
	},
	SET_SITE_GRAY: (state, d) => {
		if (!d) {
			document.body.removeAttribute('style');
		} else {
			document.body.style.filter = 'grayscale(1)';
		}
	},
	SET_SITE_INFO: (state, d) => {
		state.siteInfo = d;
		Vue.prototype.$local.set('siteInfo', d);
	},
	SET_USER_INFO: (state, d) => {
		state.userInfo = d;
		Vue.prototype.$local.set('userInfo', d);
	},
	SET_MENU_DATA: (state, d) => {
		state.menuData = d;
		Vue.prototype.$local.set('menuList', d);
	},
	SET_AUTH_BUTTON: (state, d) => {
		state.authButton = d;
		Vue.prototype.$local.set('authButton', d);
	},
	SET_SITE_THEME: (state, d) => {
		state.siteTheme = d;
	},
	SET_COURSE_LABEL: (state, d) => {
		state.courseLabel = d;
		Vue.prototype.$local.set('courseLabel', d);
	}
}

export default { state, actions, mutations }